﻿//  --------------------------------
//  Copyright (c) PK Logix LLC. All rights reserved.
//  This source code is made available under the terms of the GNU General Public License version 2 (GPLv2)
//  http://www.codeplex.com/mvcclubsite/license
//  ---------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MVCClubSite.IDAL;
using MVCClubSite.Model;
using MVCClubSite.BLL;
using MVCClubSite.Web.ViewData;
using MVCClubSite.Helpers;
using System.Web.Security;
using MVCClubSite.Web.Helpers;

namespace MVCClubSite.Web.Controllers
{
    public class EventsController : BaseController
    {
        EventsService _eventsService;
        LocationsService _locationsService;

        public EventsController()
            : this(null, null)
        {
            _eventsService = new EventsService();
            _locationsService = new LocationsService();
        }

        public EventsController(EventsService eventsService, LocationsService locationsService)
        {
            _eventsService = eventsService ?? new EventsService();
            _locationsService = locationsService ?? new LocationsService();
        }

        [AcceptVerbs("GET")]
        public ActionResult List(int? page)
        {
            EventsListViewData data = new EventsListViewData();
            int index = page.HasValue ? page.Value - 1 : 0;

            data.RecentEvents  = _eventsService.GetRecentEvents(Config.Events.RecentCount);
            data.UpCommingEvents = _eventsService.GetUpcomingEvents().ToPagedList(index, Config.Events.PageSize);

            return View("List", data);
        }

        [AcceptVerbs("GET")]
        public ActionResult Recent(int? page)
        {
            EventsListViewData data = new EventsListViewData();
            data.RecentEvents = _eventsService.GetRecentEvents(30);

            return View("Recent", data);
        }

        [AcceptVerbs("GET")]
        public ActionResult Calendar(string month, string year)
        {
            EventsCalendarViewData data = new EventsCalendarViewData();

            int m = int.Parse(month);
            int y = int.Parse(year);

            data.VisibleDate = new DateTime(y, m, 1);
            data.PreviousVisibleDate = data.VisibleDate.AddMonths(-1);
            data.NextVisibleDate = data.VisibleDate.AddMonths(1);

            DateTime startDate = CalendarHelper.FirstCalendarDay(m, y);

            for (int i = 0; i <= 41; i++)
            {
                DateTime currentDate = startDate.AddDays(i);
                Day newDay = new Day();
                newDay.DayNumber = currentDate.Day;
                newDay.Events = _eventsService.GetEventsByDate(currentDate);
                if (currentDate.Month != m)
                    newDay.Style = "othermonth";
                else if (currentDate.DayOfWeek == DayOfWeek.Saturday || currentDate.DayOfWeek == DayOfWeek.Sunday)
                    newDay.Style = "weekend";
                else
                    newDay.Style = "weekday";

                data.Days.Add(newDay);
            }


            return View("Calendar", data);
        }

        [AcceptVerbs("GET")]
        public ActionResult New()
        {
            EventViewData data = new EventViewData();
            data.Locations = _locationsService.GetLocations();
            return View("New", data);
        }

        [AcceptVerbs("POST")]
        public ActionResult Create(FormCollection form)
        {
            Event newEvent = new Event();
            this.UpdateModel(newEvent, new[] { "Title", "Description", "StartDateTime", "EndDateTime", "LocationID" });
            newEvent.Author = (Guid)Membership.GetUser().ProviderUserKey;
            _eventsService.Save(newEvent);

            return RedirectToAction("Calendar", "Events");
        }

        [AcceptVerbs("GET")]
        public ActionResult Details(string id)
        {
            int eventID = int.Parse(id);
            EventViewData data = new EventViewData();
            data.Event = _eventsService.GetEventByID(eventID);

            if (data.Event.LocationID != 0)
                data.Locations.Add(_locationsService.GetLocationByID(data.Event.LocationID));

            return View("Details", data); 
        }

        [AcceptVerbs("GET")]
        public ActionResult Edit(string id)
        {

            return View("Edit");
        }

        [AcceptVerbs("POST")]
        public ActionResult Update(FormCollection form, int id)
        {

            return RedirectToAction("List");
        }

        public string ExamineTextBox(string textBox1)
        {
            if (textBox1 != "Initial Data")
            {
                return "This text is MVC different from before!";
            }
            return String.Empty;
        }
        
    }
}
